package Action.Manager;

import Action.Sys.Setting;
import Bean.BookInfoEntity;
import Bean.LoanBookLogRecord;
import Bean.UserinfoEntity;
import com.opensymphony.xwork2.ActionSupport;
import org.hibernate.Session;
import tools.HibernateTools;
import tools.TimeTools;

import java.util.LinkedList;
import java.util.List;

public class OrderManager extends ActionSupport {
    List<LoanBookLogRecord> loanBookLogRecords;
    LinkedList<Object[]> data=new LinkedList<>();
    String key;
    String state;
    Session session;
    public String show_order()throws Exception{


        loanBookLogRecords=session.createQuery("from LoanBookLogRecord order by id desc ").getResultList();
//        HibernateTools.commit(session);
//        build_order();
        return SUCCESS;
    }

    public String find_order() throws Exception{
        session = HibernateTools.getSession();
        if (key==null||key.equals(""))
            show_order();
        else {
            loanBookLogRecords = session.createQuery("from LoanBookLogRecord where cast(id AS string ) like ? " +
                    " or  cast( userId as string ) like ? or cast(bookId as string )like ? order by id desc  ")
                    .setParameter(0, "%" + key + "%")
                    .setParameter(1, "%" + key + "%")
                    .setParameter(2, "%" + key + "%")
                    .getResultList();

        }
       if (state!=null&&!state.equals("")){
           filter_order();//订单过滤器
       }
        build_order();
//        HibernateTools.commit(session);
        return SUCCESS;
    }
    //为前台提供用户图书，借阅三个信息
    private void build_order() throws Exception{
        data.clear();
        Session session=HibernateTools.getSession();
        for (LoanBookLogRecord loanBookLogRecord : loanBookLogRecords) {
            String id=""+loanBookLogRecord.getId();
            BookInfoEntity bookInfoEntity = session.get(BookInfoEntity.class, loanBookLogRecord.getBookId());
            String bookname= bookInfoEntity==null?"已删除图书ID:"+loanBookLogRecord.getUserId():bookInfoEntity.getBook_name();
            UserinfoEntity userinfoEntity = session.get(UserinfoEntity.class, loanBookLogRecord.getUserId());
            String username= userinfoEntity==null?"已删除用户ID:"+loanBookLogRecord.getUserId():""+userinfoEntity.getUsername();
            String orderTime= TimeTools.timeStamp2Date(loanBookLogRecord.getLoanTime(),null);
            String state=loanBookLogRecord.getState();
            String day=""+loanBookLogRecord.getDay();
            Integer statepercentage=0;
            if (state.equals(Setting.LoanBook_state_closed)){
                statepercentage=100;
            }else if (state.equals(Setting.LoanBook_state_reservation))
            {
                statepercentage=25;
            }else if (state.equals(Setting.LoanBook_state_need_TimeOut)||state.equals(Setting.LoanBook_state_need_Return)){
                statepercentage=75;
            }else {
                statepercentage=100;//用户取消等情况
            }
            int remainingDays = loanBookLogRecord.remainingDays();
            if (remainingDays<0) {
                remainingDays = 0;
                if (state.equals(Setting.LoanBook_state_need_Return)){
                    loanBookLogRecord.setState(Setting.LoanBook_state_need_TimeOut);
                    state=loanBookLogRecord.getState();
                    session.update(loanBookLogRecord);
                }
            }
            data.add(new Object[]{id,username,bookname,orderTime,day,remainingDays,state,statepercentage,loanBookLogRecord.getUserId(),loanBookLogRecord.getBookId()});

        }
        if (data.size()==0){
            addActionError("没有订单信息");
        }
        HibernateTools.commit(session);

    }

    public String filter_order(){//筛选订单信息
        if (state==null||state.equals("All"))
            return SUCCESS;
//        find_order();

        LinkedList<LoanBookLogRecord> middle=new LinkedList<>();
        for (LoanBookLogRecord loanBookLogRecord : loanBookLogRecords) {
            if (loanBookLogRecord.getState().equals(state)){
                middle.addLast(loanBookLogRecord);
            }else if (state.equals(Setting.LoanBook_state_need_Return)&&loanBookLogRecord.getState().equals(Setting.LoanBook_state_need_TimeOut)){
                middle.addLast(loanBookLogRecord);
            }else if (state.equals(Setting.LoanBook_state_closed)&&loanBookLogRecord.getState().equals(Setting.LoanBook_state_cancel)){
                middle.addLast(loanBookLogRecord);
            }
        }
        loanBookLogRecords=middle;
//        build_order();
        return SUCCESS;
    }

    public List<LoanBookLogRecord> getLoanBookLogRecords() {
        return loanBookLogRecords;
    }

    public void setLoanBookLogRecords(List<LoanBookLogRecord> loanBookLogRecords) {
        this.loanBookLogRecords = loanBookLogRecords;
    }



    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public List<Object[]> getData() {
        return data;
    }

    public void setData(LinkedList<Object[]> data) {
        this.data = data;
    }

    public void setState(String state) {
        this.state = state;
    }

    public String getState() {
        return state;
    }
}
